<template>
  <div class='role-table'>
    <el-dialog
      :title='!roleForm.id ? '新增' : '修改''
      :visible.sync='roleAddDialogVisible'
       width='50%' :center='true'>
      <el-form :model='roleForm' ref='roleForm' label-width='80px'>
          <el-form-item prop='name' label='角色名称' size='mini'>
            <el-input v-model='roleForm.name' placeholder='歌手名'></el-input>
          </el-form-item>
        <el-form-item prop='name' label='角色编码' size='mini'>
          <el-input v-model='roleForm.code' placeholder='角色编码'></el-input>
        </el-form-item>
        <el-form-item prop='description' label='描述' size='mini'>
          <el-input v-model='roleForm.description' placeholder='描述'></el-input>
        </el-form-item>
      </el-form>
      <span slot='footer'>
        <el-button size='mini' type='warning' @click='roleAddDialogVisible=false'>取消</el-button>
        <el-button size='mini'  type='primary' @click='addRole'>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'role-add-or-update',
  data () {
    return {
      roleAddDialogVisible: false,
      roleForm: {
        id: '',
        name: '',
        code: '',
        description: ''
      }
    }
  },
  methods: {
    inputHandler (url) {
      console.log(url)
      console.log(1111)
      this.roleForm.pic = url
    },
    init (id) {
      console.log(id)
      this.roleForm.id = id || 0
      this.roleAddDialogVisible = true
      this.$nextTick(() => {
        this.$refs['roleForm'].resetFields()
        if (this.roleForm.id) {
          this.$api.getRoleById({
            params: {id: id}
          }).then((res) => {
            console.log(res.data)
            if (res.data && res.data.code === 200) {
              console.log(22222)
              this.roleForm.name = res.data.data.name
              this.roleForm.code = res.data.data.code
              this.roleForm.description = res.data.data.description
            }
          })
        }
      })
    },
    addRole () {
      let that = this
      let params = {
        name: this.roleForm.name,
        code: this.roleForm.code,
        description: this.roleForm.description
      }
      if (!this.roleForm.id) {
        this.$api.addRole(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.roleAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      } else {
        params.id = this.roleForm.id
        this.$api.updateRole(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.roleAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      }
    }
  }
}
</script>

<style scoped>
  .role-table{
    margin: auto;
    width: 80%;
  }
  .role-img{
    width: 100px;
    height: 100px;
    border-radius: 5px;
    overflow: hidden;
  }
  body .el-table th.gutter{
    display: table-cell!important;
  }

</style>
